package org.wjw;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/json")
public class JsonServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求编码集
        req.setCharacterEncoding("utf-8");
        //解析body
        byte[] bytes = new byte[req.getContentLength()];
        req.getInputStream().read(bytes);
        //通过byte数组来创建一个String对象
        String json = new String(bytes, StandardCharsets.UTF_8);
        //打印该字符串
        System.out.println(json);
        //返回响应
        resp.setContentType("application/json;charset = utf-8");
        resp.getWriter().write(json);




    }
}
